package com.digiassn.blogspot.birt.emitters.divemitter.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Stack;

import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.InstanceID;
import org.eclipse.birt.report.engine.content.IContainerContent;
import org.eclipse.birt.report.engine.content.IDataContent;
import org.eclipse.birt.report.engine.content.IReportContent;
import org.eclipse.birt.report.engine.content.ITextContent;
import org.eclipse.birt.report.engine.content.impl.CellContent;
import org.eclipse.birt.report.engine.content.impl.ImageContent;
import org.eclipse.birt.report.engine.content.impl.RowContent;
import org.eclipse.birt.report.engine.content.impl.TableContent;
import org.eclipse.birt.report.engine.emitter.ContentEmitterAdapter;
import org.eclipse.birt.report.engine.emitter.IEmitterServices;

public class DivEmitterImpl extends ContentEmitterAdapter {
	private OutputStream output;
	private OutputStreamWriter writer;
	private Stack<String> parents;
	private Integer lastGeneratedID;
	
	@Override
	public void initialize(IEmitterServices service) throws BirtException {
		try {
			super.initialize(service);
			
			parents = new Stack<String>();
			
			IRenderOption option = service.getRenderOption();

			String outputFileName = option.getOutputFileName();
			
			if (outputFileName != null)
			{
				FileOutputStream fileOut = new FileOutputStream(new File(outputFileName));
				output = fileOut;
			}
			else
			{
				output = option.getOutputStream();
			}
			
			writer = new OutputStreamWriter(output);
		} catch (FileNotFoundException e) {
			throw new BirtException(e.getMessage());
		}
	}

	@Override
	public void start(IReportContent report) throws BirtException {
		// TODO Auto-generated method stub
		super.start(report);
	}

	@Override
	public void startContainer(IContainerContent container)
			throws BirtException {
		try {
			super.startContainer(container);
			
			String divName = container.getName();
		
			if (divName == null)
			{
				if (container instanceof CellContent)
				{
					divName = "Cell";
				}
				
				if (container instanceof TableContent)
				{
					divName = "Table";
				}
				
				if (container instanceof RowContent)
				{
					divName = "Row";
				}
				
				if (container instanceof ImageContent)
				{
					divName = "Image";
				}
			}
			
			String parent = null;
			if (parents.size() > 0)
			{
				parent = parents.peek();
			}
			
			StringBuilder sb = new StringBuilder();
			
			if (parent != null)
			{
				sb.append(parent);
				sb.append("-");
			}
			
			sb.append(divName);
			sb.append("-");
			if (lastGeneratedID == null)
			{
				lastGeneratedID = 0;
			}
			if (container != null)
			{
				InstanceID instanceId = container.getInstanceID();
				
				if (instanceId != null)
				{
					sb.append(container.getInstanceID().getUniqueID());
				}
				else
				{
					sb.append("conGen-" + lastGeneratedID);
					lastGeneratedID++;
				}
			}
			else
			{
				sb.append("gen-" + lastGeneratedID);
				lastGeneratedID++;
			}
			
			String ID = sb.toString();
			
			parents.push(ID);
			
			sb = new StringBuilder();
			sb.append("<div ");
			sb.append("id='");
			sb.append(ID);
			sb.append("' name='");
			sb.append(divName);
			sb.append("' class='");
			sb.append(divName);
			
			if (container.getStyleClass() != null)
			{
				sb.append(" " + container.getStyleClass());
			}
			
			sb.append("'>");
			
			if (container instanceof CellContent)
			{
				CellContent cellContent = (CellContent) container;
				
				if (cellContent.hasChildren() == false)
				{
					sb.append("<div class='Data'>&nbsp;</div>");
				}
			}
			
			writer.write(sb.toString());
		} catch (IOException e) {
			throw new BirtException(e.getMessage());
		}
	}

	
	
	@Override
	public void startText(ITextContent text) throws BirtException {
		// TODO Auto-generated method stub
		try {
			super.startText(text);
			
			StringBuilder sb = new StringBuilder();
			
			sb.append("<div id='");
			if (parents.size() > 0)
			{
				sb.append(parents.peek() + "-");
			}
			
			sb.append("Data" + text.getInstanceID().getUniqueID());
			sb.append("' name='Data' class='Data ");
			sb.append(text.getStyleClass());
			sb.append("'>");
			
			String dataText = "&nbsp;";
			
			if (text.getText() != null)
			{
				dataText = text.getText();
			}
			
			writer.write(sb.toString() + dataText + "</div>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void endContainer(IContainerContent container) throws BirtException {
		try {
			super.endContainer(container);
			parents.pop();
			writer.write("</div>");
		} catch (IOException e) {
			throw new BirtException(e.getMessage());
		}
	}

	@Override
	public void end(IReportContent report) throws BirtException {
		try {
			super.end(report);
			
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
